<template>
    <!-- <div>弃用</div> -->
    <el-dialog
        :title="!dataForm.id ? $t('handle.add') : $t('handle.update')"
        :close-on-click-modal="false"
        :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="200px">

        <el-form-item label="小区全称" prop="fullName">
            <el-input v-model="dataForm.fullName" placeholder="小区全称"></el-input>
        </el-form-item>

        <el-form-item label="简称" prop="shortName">
            <el-input v-model="dataForm.shortName" placeholder="简称"></el-input>
        </el-form-item>
        <el-form-item label="小区编码" prop="code">
                <el-input v-model="dataForm.code" placeholder="小区编码"></el-input>
        </el-form-item>
        <el-form-item label="省/市/区/社区/居委会" >
            <el-cascader ref="location" v-model="address" :options="options" @click.native="getCountry" @active-item-change="getNodes" :props="props" style="width:100%;"></el-cascader>
        </el-form-item>

        <!-- <el-form-item label="所属社区" prop="communityCode">
            <el-input v-model="dataForm.communityCode" placeholder="所属社区"></el-input>
        </el-form-item>
        <el-form-item label="所属居委会" prop="neighborhoodCommitteeCode">
            <el-input v-model="dataForm.neighborhoodCommitteeCode" placeholder="所属居委会"></el-input>
        </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="callOf()">{{ $t('handle.cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmit()">{{ $t('handle.confirm') }}</el-button>
    </span>
  </el-dialog>
</template>
<script>
export default {
    data(){
        // 严格的验证规则可以放到这里验证

        return {
            visible: false,
            type: '',
            dataForm:{
                id:'',
                fullName:'',
                shortName:'',
                code: '',
                communityCode: '',
                neighborhoodCommitteeCode: '',
                country: '',
                provinceCode: '',
                cityCode: '',
                areaCode: '',
                dgStandardInfoId: ''
                // 省市区小区编码后续再加上
            },
            address: [],
            options: [],
            props: {
                value: "id",
                label: "name",
                children: "children"
            },
            // 验证Todo 其他的验证后续加上
            dataRule:{
                fullName:[
                    {required: true, message: '全称不能为空', trigger: 'blur'}
                ],
                shortName:[
                    {required: true, message: '简称不能为空', trigger: 'blur'}
                ],
                code: [
                    {required: true, message: '编码不能为空', trigger: 'blur'}
                ],
                communityCode: [
                    {required: true, message: '地址不能为空', trigger: 'blur'}
                ],
                cityCode: [
                   {required: false, message: '市不能为空', trigger: 'change'}
                ],
                areaCode: [
                    {required: false, message: '区不能为空', trigger: 'change'}
                ]
            }
        }
    },
    created(){
        // this.getCountry()
    },
    methods: {
        init(id,type){
            this.type = type
            this.visible = true
            this.dataForm.id = id || 0
            if(this.dataForm.id){
                this.$http({
                url: `/dg-ic/ic/housing-estate/info/${this.dataForm.id}`,
                method: 'get',
                params: this.$http.adornParams()
                }).then(({data})=>{
                    if(data && data.code === 0){
                        this.dataForm.fullName = data.data.fullName
                        this.dataForm.shortName = data.data.shortName
                        this.dataForm.code = data.data.code
                        this.options = [{
                            id: data.data.neighborhoodCommitteeCode,
                            name: data.data.housingEstateName
                        }];
                        this.address = [data.data.neighborhoodCommitteeCode];
                    }
                })
            }
        },
        // 表单提交
        dataFormSubmit(){
            this.$refs['dataForm'].validate((validate)=>{
                if(validate){
                    this.$http({
                        url: `/dg-ic/ic/housing-estate/${!this.dataForm.id ? 'save' : 'update'}`,
                        method: 'post',
                        data: this.$http.adornData({
                            'id':this.dataForm.id || undefined,
                            'fullName': this.dataForm.fullName,
                            'shortName': this.dataForm.shortName,
                            'code': this.dataForm.code,
                            'communityCode': this.address[this.address.length-2],
                            'neighborhoodCommitteeCode': this.address[this.address.length-1],
                            'provinceCode': this.address[this.address.length-5],
                            'cityCode': this.address[this.address.length-4],
                            'areaCode': this.address[this.address.length-3],
                            'dgStandardInfoId': this.address[this.address.length-1],
                            'createBy': this.$store.state.user.id,
                            'updateBy': this.$store.state.user.id
                            // 后续的字段后续加上
                        })
                    }).then(({data})=>{
                        if(data && data.code === 0){
                            this.$message({
                            message: '操作成功',
                            type: 'success',
                            duration: 1500,
                            onClose: () => {
                                this.visible = false
                                this.$emit('refreshDataList')
                            }
                        })
                        }else{
                            this.$message.console.error(data.msg);
                        }
                    })
                }
            })
        },
        // 国家
        getCountry(){
            this.$http({
                url: `/dg-standard-info/dgstandardaddress/childlistbyparentandtype`,
                method: 'post',
                params:{parentId: 0,type:11}
            }).then(({data})=>{
                if(data && data.code === 0){
                    data.data.map(item => {
                        this.$set(item, "name", item.name);
                        this.$set(item, "children", []);
                    });
                    this.options = data.data

                }
            })
        },
        // 获取省份
        getProviceCode(countryId){
            this.$http({
                url: `/dg-standard-info/dgstandardaddress/childlistbyparentandtype`,
                method: 'post',
                params:{parentId: countryId,type:12}
            }).then(({data})=>{
                if(data && data.code === 0){
                    data.data.map(item => {
                        this.$set(item, "name", item.name);
                        this.$set(item, "children", []);
                    });
                   this.options.map(item=>{
                       if(item.id === countryId){
                           item.children = data.data
                       }
                   })
                }
            })
        },
        // 获取市
        getCityCode(countryId,provinceId){
            this.$http({
                url: `/dg-standard-info/dgstandardaddress/childlistbyparentandtype`,
                method: 'post',
                params:{parentId: provinceId,type:13}
            }).then(({data})=>{
                if(data && data.code === 0){
                    data.data.map(item => {
                        this.$set(item, "name", item.name);
                        this.$set(item, "children", []);
                    });
                   this.options.map((item, i) => {
                        if (item.id === countryId) {
                            item.children.map((city, id) => {
                                 city.children = data.data;
                            });
                        }
                    });
                }
            })
        },
        // 获取区
        getAreaCode(countryId,provinceId,cityId){
            this.$http({
                url: `/dg-standard-info/dgstandardaddress/childlistbyparentandtype`,
                 method: 'post',
                params:{parentId: cityId,type:31}
            }).then(({data})=>{
                if(data && data.code === 0){
                    data.data.map(item => {
                        this.$set(item, "name", item.name);
                        this.$set(item, "children", []);
                    });
                    this.options.map((item, i) => {
                        if (item.id === countryId) {
                        item.children.map((provice, idx) => {
                            if(provice.id === provinceId){
                                provice.children.map((city, ids) => {
                                    city.children = data.data
                                });
                            }
                        });
                        }
                    });
                }

            })
        },
        // 获取社区
        getCommunityCode(countryId,provinceId,cityId,areaId){
            this.$http({
                url: `/dg-standard-info/dgstandardaddress/childlistbyparentandtype`,
                 method: 'post',
                params:{parentId: areaId,type:32}
            }).then(({data})=>{
                if(data && data.code === 0){
                    data.data.map(item => {
                        this.$set(item, "name", item.name);
                        // this.$set(item, "children", []);
                    });
                    this.options.map((item, i) => {
                        if (item.id === countryId) {
                        item.children.map((provice, idx) => {
                            if(provice.id === provinceId){
                                provice.children.map((city, ids) => {
                                    if(city.id === cityId){
                                        city.children.map((area,aid)=>{
                                             area.children = data.data
                                        })
                                    }
                                });
                            }
                        });
                        }
                    });
                }

            })
        },
        // 获取居委会 社区id
        getNeighborhoodCommitteeCode(countryId,provinceId,cityId,areaId,communityCodeId){
            this.$http({
                url: `/dg-standard-info/dgstandardaddress/childlistbyparentandtype`,
                method: 'post',
                params:{parentId: communityCodeId,type:32}
            }).then(({data})=>{
                if(data && data.code === 0){
                    data.data.map(item => {
                        this.$set(item, "name", item.name);
                        // this.$set(item, "children", []);
                    });
                    this.options.map((item, i) => {
                        if (item.id === countryId) {
                        item.children.map((provice, idx) => {
                            if(provice.id === provinceId){
                                provice.children.map((city, ids) => {
                                    if(city.id === cityId){
                                        city.children.map((area,aid)=>{
                                            if(area.id === areaId){
                                                area.children.map((communt,ic)=>{
                                                    communt.children = data.data
                                                })
                                            }
                                        })
                                    }
                                });
                            }
                        });
                        }
                    });
                }

            })
        },
        getNodes(val){
            if(val.length === 1){// 获取省
                this.getProviceCode(val[0])
            }else if(val.length === 2){// 根据省份获取市
                this.getCityCode(val[0],val[1])
            }else if(val.length === 3){// 区
                this.getAreaCode(val[0],val[1],val[2])
            }else if(val.length === 4){
                this.getCommunityCode(val[0],val[1],val[2],val[3])
            }else if(val.length === 5){
                this.getNeighborhoodCommitteeCode(val[0],val[1],val[2],val[3],val[4])
            }
        },
        callOf(){
            this.visible = false
            this.$refs[`dataForm`].resetFields()
            let obj = {}
            obj.stopPropagation = () =>{}
            this.$refs.location.clearValue(obj)
        }
    }
}
</script>
